package cn.zx.rmq.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

/**
 * @Package: cn.zx.rmq.producer
 * @ClassName: SyncProducer
 * @Author: Think
 * @CreateTime: 2021/6/28 10:58
 * @Description: 消息还是立即就发送给MQ，只不过MQ会根据设置，将消息延迟被消费
 */
@Slf4j
public class DelayProducer {

    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException, UnsupportedEncodingException {
        DefaultMQProducer producer = new DefaultMQProducer("DelayProducer-Group");
        producer.setNamesrvAddr("localhost:9876");
        producer.start();

        for (int i = 0; i < 5; i++) {
            Message msg = new Message("KYC", "about-money", ("客户拥有金额为：" + i*1000).getBytes(RemotingHelper.DEFAULT_CHARSET));
            // 设置消息的延迟等级1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
            msg.setDelayTimeLevel(4);
            SendResult sendResult = producer.send(msg);
            log.info("发送返回信息为：{}", sendResult);
        }

        producer.shutdown();
    }

}
